//
//  LoginViewController.m
//  LOU
//
//  Created by iMacOne on 15/11/6.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "LoginViewController.h"
#import "Size.h"
#import "ToolUtil.h"
#import "RegisterViewController.h"
#import "LostPasswardViewController.h"
#import "WZFlashButton.h"
#import "LeafNotification.h"
@interface LoginViewController ()
@property (nonatomic, strong)UITextField * phoneNumberTF;
@property (nonatomic, strong)UITextField * passwordTF;
@property (nonatomic, strong)UIImageView * phoneImageView;
@property (nonatomic, strong)UIImageView * passwordImageView;
@property (nonatomic, strong)WZFlashButton * loginButton;
@property (nonatomic, strong)UIButton * registerButton;
@property (nonatomic, strong)UIButton * lostButton;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    [ToolUtil createLine:margin_64 superView:self.view lift:0 width:WindowWidth hight:0.5 color:[UIColor grayColor]];

//    self.view.backgroundColor = [UIColor cyanColor];
    [self createTitle:@" " midTitle:@"登录"];
    [self createView];
    
    
}

//- (void)viewWillDisappear:(BOOL)animated
//{
//    [super viewWillDisappear:YES];
//    self.tabBarController.tabBar.hidden = NO;
//}
- (void)createView
{
    self.phoneNumberTF = [[UITextField alloc] initWithFrame:CGRectMake((WindowWidth - margin_250)/ 2, margin_48 + 64, margin_250,margin_34)];
//        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    _phoneNumberTF.placeholder = @" 请输入手机号码";
    _phoneNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneNumberTF.backgroundColor = [UIColor whiteColor];
    _phoneNumberTF.inputAccessoryView = nil;
    _phoneNumberTF.font = FONT(14);
    _phoneNumberTF.delegate = self;
    [self.view addSubview:_phoneNumberTF];
    [ToolUtil createLine:CGRectGetMaxY(_phoneNumberTF.frame) + 1 superView:self.view lift:_phoneNumberTF.frame.origin.x width:_phoneNumberTF.frame.size.width hight:0.5 color:[UIColor grayColor]];
    
    self.passwordTF = [[UITextField alloc] initWithFrame:CGRectMake((WindowWidth - margin_250)/ 2, CGRectGetMaxY(_phoneNumberTF.frame) + 10, margin_250,margin_34)];
    //        _phoneNumberTF.borderStyle = UITextBorderStyleRoundedRect;
    _passwordTF.placeholder = @"请输入密码";
    _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwordTF.backgroundColor = [UIColor whiteColor];
    _passwordTF.inputAccessoryView = nil;
    _passwordTF.font = FONT(14);
    _passwordTF.delegate = self;
    [self.view addSubview:_passwordTF];
    [ToolUtil createLine:CGRectGetMaxY(_passwordTF.frame) + 1 superView:self.view lift:_passwordTF.frame.origin.x width:_passwordTF.frame.size.width hight:0.5 color:[UIColor grayColor]];
    
    self.loginButton = [[WZFlashButton alloc] initWithFrame:CGRectMake(_passwordTF.frame.origin.x, CGRectGetMaxY(_passwordTF.frame)+10, _passwordTF.frame.size.width, margin_34)];
    //_loginButton.frame = ;
    _loginButton.layer.masksToBounds = YES;
    _loginButton.layer.cornerRadius = margin_5;
    _loginButton.backgroundColor = [UIColor blackColor];
    _loginButton.textLabel.font = FONT(14);
    _loginButton.tag = 200;
    [_loginButton setText:@"登录" withTextColor:[UIColor whiteColor]];
    //[_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    //[_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginButton.clickBlock = ^void{
        if (_loginButton.tag == 200) {
            if ([ToolUtil isBlank:_phoneNumberTF.text] || [ToolUtil isBlank:_passwordTF.text] ) {
               // [self prompt:@"账号和密码不能为空"];
                [LeafNotification showInController:self withText:@"账号和密码不能为空"];
                [self lockAnimationForView:_passwordTF];
                [self lockAnimationForView:_phoneNumberTF];
                
            }else {
                
                
                
            }
        }else if (_loginButton.tag == 201){
            [self jump:@"" to:[[RegisterViewController alloc] init]];
        }else if (_loginButton.tag == 202){
            NSLog(@"wangji");
            [self jump:@"ggg" to:[[LostPasswardViewController alloc] init]];
        }

    };
    //[_loginButton addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    
    self.registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _registerButton.frame = CGRectMake(_passwordTF.frame.origin.x, CGRectGetMaxY(_loginButton.frame)+10, _passwordTF.frame.size.width, margin_34);
    _registerButton.layer.masksToBounds = YES;
    _registerButton.layer.cornerRadius = margin_5;
    _registerButton.backgroundColor = [UIColor blackColor];
    _registerButton.titleLabel.font = FONT(14);
    _registerButton.tag = 201;
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_registerButton addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_registerButton];
    
    self.lostButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _lostButton.frame = CGRectMake((WindowWidth - 60) / 2, CGRectGetMaxY(_registerButton.frame)+10, 60, margin_34);
    _lostButton.layer.masksToBounds = YES;
    _lostButton.layer.cornerRadius = margin_5;
    _lostButton.backgroundColor = [UIColor blackColor];
    _lostButton.titleLabel.font = FONT(14);
    _lostButton.tag = 202;
    [_lostButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [_lostButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_lostButton addTarget:self action:@selector(doClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_lostButton];
    
}
-(void)lockAnimationForView:(UIView*)view
{
    CALayer *lbl = [view layer];
    CGPoint posLbl = [lbl position];
    CGPoint y = CGPointMake(posLbl.x-10, posLbl.y);
    CGPoint x = CGPointMake(posLbl.x+10, posLbl.y);
    CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction
                                  functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    [animation setAutoreverses:YES];
    [animation setDuration:0.08];
    [animation setRepeatCount:3];
    [lbl addAnimation:animation forKey:nil];
}

- (void)doClickButton:(UIButton *)btn
{
    
    if (btn.tag == 200) {
        if ([ToolUtil isBlank:_phoneNumberTF.text] || [ToolUtil isBlank:_passwordTF.text] ) {
            [self prompt:@"账号和密码不能为空"];
        }else {
            
        }
    }else if (btn.tag == 201){
        [self jump:@"" to:[[RegisterViewController alloc] init]];
    }else if (btn.tag == 202){
        NSLog(@"wangji");
        [self jump:@"ggg" to:[[LostPasswardViewController alloc] init]];
    }
}
// 失去焦点隐藏键盘
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
    [textField resignFirstResponder];
    return YES;
}

@end
